@tool
extends PanelContainer

class_name InventoryGrid

@export_tool_button("Populate") var populate_btn = test_populate

const InventorySlot = preload("res://scenes/ui/inventory/inventory_slot.tscn")
const TEST_INVENTORY = preload("res://resources/inventory/items/test_inventory.tres")

@onready var grid_container: GridContainer = $VBoxContainer/MarginContainer/GridContainer

#func _ready() -> void:
	#var inventory_data = preload("res://resources/items/test_inventory.tres")
	#populate_grid(inventory_data.item_datas)

func populate_item_grid(inventory_data: InventoryData, tool_call = false) -> void:
	for child in grid_container.get_children():
		child.queue_free()
	
	for slot_data in inventory_data.slot_datas:
		var slot = InventorySlot.instantiate()
		grid_container.add_child(slot)
		
		if tool_call:
			continue
		slot.slot_clicked.connect(inventory_data.on_slot_clicked)
		
		if slot_data:
			slot.set_slot_data(slot_data)

func test_populate() -> void:
	populate_item_grid(TEST_INVENTORY, true)
